package com.ldl.origami.origamiService;

import com.ldl.origami.dao.UserScoreMapper;
import com.ldl.origami.domain.UserScore;
import com.ldl.origami.domain.UserScoreExample;
import com.ldl.origami.util.OrigamiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能描述:
 * 作者: LDL
 * 创建时间: 2014/9/2 20:44
 */
@Service
public class UserScoreService {

    @Autowired(required = false)
    private UserScoreMapper userScoreMapper;

    static Logger logger = LoggerFactory.getLogger(UserScoreService.class);


    /**
     * 保存用户对教程的评分
     * @param userScore
     * @throws Exception
     */
    public void addScore(UserScore userScore) throws Exception {

        try {
            userScoreMapper.insert(userScore);
        } catch (Exception e) {
            logger.debug("插入用户评分失败:"+ OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }

    }

    /**
     * 根据教程ID和用户名查询评分情况
     * @param userName
     * @param tutorialId
     * @return
     * @throws Exception
     */
    public List<UserScore> getUserScore(String userName,int tutorialId) throws Exception {
        UserScoreExample example = new UserScoreExample();
        example.createCriteria().andUserNameEqualTo(userName).andTutorialIdEqualTo(tutorialId);
        List<UserScore> userScores = null;
        try {
            userScores = userScoreMapper.selectByExample(example);
        } catch (Exception e) {
            logger.debug("查询教程评分情况出错: "+OrigamiUtils.getErrorInfoFromException(e
            ));
            throw new Exception();
        }

        return userScores;
    }
}
